package com.lyl.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.domain.SysPrivilege;
import com.lyl.model.Result;
import com.lyl.service.SysPrivilegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @author lylbell
 * @date 2021-06-22 19:29
 *
 * 查询 sys_privilege_query
 * 增加 sys_privilege_create
 * 修改 sys_privilege_update
 * 删除 sys_privilege_delete
 */
@RestController
@Api(tags = "后台权限功能")
@RequestMapping("/privileges")
@CrossOrigin
public class SysPrivilegeController {
    
    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    /**
     * 分页查找权限
     * @return
     */
    @GetMapping
    @ApiOperation(value="分页查询权限数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小")
    })
    @PreAuthorize("hasAuthority('sys_privilege_query')")  //访问该接口，必须有sys_privilege_query的权限
    public Result<Page<SysPrivilege>> getByPage(@ApiIgnore Page<SysPrivilege> page) {
        page.addOrder(OrderItem.desc("last_update_time"));//设置排序，以last_update_time降序排列，填写字段为数据库里面的字段名。
        Page<SysPrivilege> sysPrivilegePage = sysPrivilegeService.page(page);
        return Result.ok(sysPrivilegePage);
    }

    @PostMapping
    @ApiOperation(value="增加权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysPrivilege",value = "权限的json数据")
    })
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public Result insert(@RequestBody @Validated SysPrivilege sysPrivilege) {
        boolean save = sysPrivilegeService.save(sysPrivilege);
        if(save) {
            return Result.ok("权限添加成功");
        }
        return Result.fail("权限添加失败");
    }


    @PatchMapping
    @ApiOperation(value = "更新权限")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "sysPrivilege",value = "权限的json数据")
    })
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public Result update(@RequestBody @Validated SysPrivilege sysPrivilege) {
        boolean update = sysPrivilegeService.updateById(sysPrivilege);
        if (update) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }

/*    public static void main(String[] args) {
        SysPrivilege sysPrivilege = new SysPrivilege();
        sysPrivilege.setDescription("hhahh ");
        sysPrivilege.setMenuId(9L);
        sysPrivilege.setName("jkfsf");
        SysPrivilegeController sysPrivilegeController = new SysPrivilegeController();

        Result insert = sysPrivilegeController.insert(sysPrivilege);
        System.out.println(insert);
    }*/
}
